package org.dromara.encrypt;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.encrypt.properties.EncryptorProperties;
import org.dromara.common.encrypt.utils.EncryptUtils;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 秘钥管理
 * @Author: Stars
 * @Date: 2025/5/17 21:27
 */
@RestController
@RequestMapping("/encrypt")
@RequiredArgsConstructor
public class EncryptController {

    private final EncryptorProperties encryptorProperties;

    /**
     * 获取秘钥
     * <p>
     *     这里最好添加权限验证，但是这个是App发送过来的
     * </p>
     */
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    @GetMapping
    public R<?> addGroupMember() {
        // 这是是临时秘钥，需要使用这个秘钥把正式秘钥加密
        String tempKey = encryptorProperties.getTempKey();
        String encryptByAes = EncryptUtils.encryptByAes(encryptorProperties.getAppKey() + "|" + encryptorProperties.getChatKey(), tempKey);
        return R.ok("OK", encryptByAes);
    }
}
